package com.example.wuyeguanli.activity;

import androidx.test.core.app.ActivityScenario;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.example.wuyeguanli.R;
import com.example.wuyeguanli.tools.ApiClient;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.io.IOException;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.action.ViewActions.closeSoftKeyboard;
import static androidx.test.espresso.action.ViewActions.scrollTo;
import static androidx.test.espresso.action.ViewActions.typeText;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.withClassName;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.endsWith;

@RunWith(AndroidJUnit4.class)
public class VisitorManagementActivityTest {

    private static MockWebServer mockWebServer;
    private static String originalBaseUrl;

    @BeforeClass
    public static void setUpClass() throws IOException {
        originalBaseUrl = ApiClient.BASE_URL;
        mockWebServer = new MockWebServer();
        mockWebServer.start(8080);
        ApiClient.setBaseUrlForTesting(mockWebServer.url("/").toString());
    }

    @AfterClass
    public static void tearDownClass() throws IOException {
        if (mockWebServer != null) {
            mockWebServer.shutdown();
        }
        ApiClient.setBaseUrlForTesting(originalBaseUrl);
    }

    @Test
    public void testSubmitVisitorInfo_Success() {
        // 模拟提交成功的响应
        mockWebServer.enqueue(new MockResponse().setBody("{\"state\":\"success\",\"error\":\"\",\"data\":null}"));

        ActivityScenario.launch(VisitorManagementActivity.class);

        // 填写表单
        onView(withId(R.id.edit_room)).perform(typeText("101101"));
        onView(withId(R.id.register_name)).perform(typeText("访客张三"));
        onView(withId(R.id.register_phone)).perform(typeText("13300133001"));
        onView(withId(R.id.register_reason)).perform(typeText("朋友聚会"), closeSoftKeyboard());

        // 点击提交
        onView(withId(R.id.btn_submit)).perform(scrollTo(), click());

        // 成功后页面会finish，这里很难做强断言。
        // 主要验证在执行完整流程后，App没有崩溃。
    }

    @Test
    public void testDateTimePicker_Interaction() {
        ActivityScenario.launch(VisitorManagementActivity.class);

        // 点击时间选择文本框
        onView(withId(R.id.time_text)).perform(click());

        // 验证日期选择器出现
        onView(withClassName(endsWith("DatePicker"))).check(matches(androidx.test.espresso.matcher.ViewMatchers.isDisplayed()));
        // 选择一个日期并确认
        onView(withId(android.R.id.button1)).perform(click());

        // 验证时间选择器出现
        onView(withClassName(endsWith("TimePicker"))).check(matches(androidx.test.espresso.matcher.ViewMatchers.isDisplayed()));
        // 选择一个时间并确认
        onView(withId(android.R.id.button1)).perform(click());

        // 再次验证日期选择器出现 (为结束时间)
        onView(withClassName(endsWith("DatePicker"))).check(matches(androidx.test.espresso.matcher.ViewMatchers.isDisplayed()));
        onView(withId(android.R.id.button1)).perform(click());

        // 再次验证时间选择器出现 (为结束时间)
        onView(withClassName(endsWith("TimePicker"))).check(matches(androidx.test.espresso.matcher.ViewMatchers.isDisplayed()));
        onView(withId(android.R.id.button1)).perform(click());

        // 验证时间文本已被更新
        onView(withId(R.id.time_text)).check(matches(withText(containsString("至"))));
    }
}